<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\PublicCommentController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaPayrollRequisitionExamineApprove
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_payroll_requisition_examine_approve';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    /**
     * @param $page
     * @param $pageNum
     * @param $purchasecontractsn
     * @param string[] $fields
     * @param array $condition
     * @param array $between
     * @return false|string
     */
    public static function list($adminId,$page, $pageNum,$whereNotIn, $purchasecontractsn, $fields = ['*'], $condition = [], $between = []){
        $offset = ($page - 1) * $pageNum;
        // $purchasecontractsn 为空 就是没有当前登录用户的审核订单
        if (gettype($purchasecontractsn) == "object"){
            $purchasecontractsn = get_object_vars($purchasecontractsn); // 对象转数组函数
        }elseif (gettype($purchasecontractsn) == "array"){
            $purchasecontractsn = $purchasecontractsn;
        }
        if (!$purchasecontractsn){
            $count = 0;
            $list = [];
        }else{
            if ($between) {
                $model = Db::table('wa_payroll_requisition_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('purchase_payroll_sn',$purchasecontractsn)// get_object_vars()
                    ->whereNotIn('status',$whereNotIn)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_payroll_requisition_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('purchase_payroll_sn',$purchasecontractsn)
                    ->whereNotIn('status',$whereNotIn);
            }
            $count = $model->count();
            $list = $model
                ->orderBy('id', 'desc')
                ->offset($offset)
                ->limit($pageNum)
                ->get();
        }
        $condition [] = ['is_show','=',1];


        if ($list){
            foreach ($list as $k){

                $approve = WaPayrollRequisitionExamineApproveFlow::all(
                    ['purchase_payroll_sn' => $k->purchase_payroll_sn,'pr_no' => $k->pr_no]);
                $approve = json_decode($approve);
                if ($approve) {
                    foreach ($approve as $kpr) {
                        if ($kpr->admin_ids){
                            $kpr->admin_ids = json_decode($kpr->admin_ids);
                        }
                        if ($kpr->status == 3) {
                            $approve_status = 3; // 已驳回
                            $approve_status_name = '已撤回';
                        } elseif ($kpr->status == 2) {
                            $approve_status = 2; // 已驳回
                            $approve_status_name = '已驳回';
                        } elseif ($kpr->status == 1) {
                            if (in_array($adminId->id, $kpr->admin_ids)){
                                $approve_status = 1; // 已审批
                                $approve_status_name = '已审批';
                            }else{
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } elseif ($kpr->status == 0) {
                            if (in_array($adminId->id, $kpr->admin_ids)){
                                $approve_status = 0; // 待审批
                                $approve_status_name = '待审批';
                            }else{
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } else {
                            $approve_status = 999; // 已驳回
                            $approve_status_name = '不在审批流中';
                        }
                    }
                } else {
                    $approve_status = 10001; // 审批状态
                    $approve_status_name = '没有审批记录';// 审批状态名称
                }
                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称

                $k->approve_flow = json_decode($k->approve_flow);
                // 请款审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0){
                    $k->status_name = '审批中';
                }elseif ($k->status == 1){
                    $k->status_name = '审批通过';
                }elseif ($k->status == 2){
                    $k->status_name = '审批驳回';
                }elseif ($k->status == 3){
                    $k->status_name = '撤回审批';
                }elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                }elseif ($k->status == 5) {
                    $k->status_name = '作废';
                }else{
                    $k->status_name = '';
                }
//                $k->approve_flow = $k->approve_flow;
            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    public static function waitApproveByAdminIdList($param,$adminId,$page, $pageNum, $purchasecontractsn, $fields = ['*'], $condition = [], $between = []){
        $offset = ($page - 1) * $pageNum;
        // $purchasecontractsn 为空 就是没有当前登录用户的审核订单
        if (gettype($purchasecontractsn) == "object"){
            $purchasecontractsn = get_object_vars($purchasecontractsn); // 对象转数组函数
        }elseif (gettype($purchasecontractsn) == "array"){
            $purchasecontractsn = $purchasecontractsn;
        }
        if (!$purchasecontractsn){
            $count = 0;
            $list = [];
        }else{
            /**
             * 先获取审批中 待审批的 订单编号 和对应的审批id或编号
             * 审批流中的 部门id 必须是 当前人员的部门
             * 遍历审批流中的 部门admin_ids 中必须有当前人员 有他 但是并没有审批通过的状态就需要展示（因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录）
             *
             */
            $wai_approve_sn = []; // 待当前人员审批的订单
            $where_approve = [];
            $where_approve [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
            $approve = WaPayrollRequisitionExamineApprove::all($where_approve);
            $approve = json_decode($approve);


            if ($approve) {
                foreach ($approve as $kap) {
                    $where_order = [];
                    $where_order [] = ['purchase_payroll_sn', '=', $kap->purchase_payroll_sn];
                    $order_data = WaPurchasePayrollRequisition::find($where_order);
                    $order_data = json_decode($order_data);
                    if ($order_data) {
                        PublicCommentController::请款单状态;
                        // 主订单是审批中的 才需要展示在待审批列表
                        if ($order_data->status == 1) {
                            // 现在开始查审批流
                            $where_approve_flow = [];
                            $where_approve_flow [] = ['pr_no', '=', $kap->pr_no]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                            $where_approve_flow [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                            $approve_flow = WaPayrollRequisitionExamineApproveFlow::find($where_approve_flow);
                            $approve_flow = json_decode($approve_flow);
                            if ($approve_flow) {
                                if ($approve_flow->admin_ids) {
                                    $admin_ids = json_decode($approve_flow->admin_ids);
                                    if (in_array($adminId->id, $admin_ids)) {
                                        // 这个时候把审批订单编号扔出来 就是待当前人员待审核的数据
                                        array_push($wai_approve_sn, $kap->pr_no);
                                    }
                                }
                            }

                        }
                    }
                }
            }


            PublicCommentController::请款单请款结算类型;
            if ($param['payroll_requisition_type'] == 3) {
                $model = Db::table('wa_payroll_requisition_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereNotNull('bill_number')
                    ->whereIn('pr_no',$wai_approve_sn);
            }
            if ($param['payroll_requisition_type'] == 1) {
                $model = Db::table('wa_payroll_requisition_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereNull('bill_number')
                    ->whereIn('pr_no',$wai_approve_sn);
            }

            $count = $model->count();
            $list = $model
                ->orderBy('id', 'desc')
                ->offset($offset)
                ->limit($pageNum)
                ->get();
        }
        $condition [] = ['is_show','=',1];


        if ($list){
            foreach ($list as $k){

                $approve = WaPayrollRequisitionExamineApproveFlow::all(
                    ['purchase_payroll_sn' => $k->purchase_payroll_sn, 'pr_no' => $k->pr_no]);
                $approve = json_decode($approve);
                if ($approve) {
                    foreach ($approve as $kpr) {
                        if ($kpr->admin_ids){
                            $kpr->admin_ids = json_decode($kpr->admin_ids);
                        }
                        if ($kpr->status == 3) {
                            $approve_status = 3; // 已驳回
                            $approve_status_name = '已撤回';
                        } elseif ($kpr->status == 2) {
                            $approve_status = 2; // 已驳回
                            $approve_status_name = '已驳回';
                        } elseif ($kpr->status == 1) {
                            if (in_array($adminId->id, $kpr->admin_ids)){
                                $approve_status = 1; // 已审批
                                $approve_status_name = '已审批';
                            }else{
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } elseif ($kpr->status == 0) {
                            if (in_array($adminId->id, $kpr->admin_ids)){
                                $approve_status = 0; // 待审批
                                $approve_status_name = '待审批';
                            }else{
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } else {
                            $approve_status = 999; // 已驳回
                            $approve_status_name = '不在审批流中';
                        }
                    }
                } else {
                    $approve_status = 10001; // 审批状态
                    $approve_status_name = '没有审批记录';// 审批状态名称
                }

                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称

                $k->approve_flow = json_decode($k->approve_flow);
                // 请款审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0){
                    $k->status_name = '审批中';
                }elseif ($k->status == 1){
                    $k->status_name = '审批通过';
                }elseif ($k->status == 2){
                    $k->status_name = '审批驳回';
                }elseif ($k->status == 3){
                    $k->status_name = '撤回审批';
                }elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                }elseif ($k->status == 5) {
                    $k->status_name = '作废';
                }else{
                    $k->status_name = '';
                }
//                $k->approve_flow = $k->approve_flow;
            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * 采购请款审批-状态修改
     * @param $data
     * @return int
     */
    public static function updated($condition,$data)
    {
        return Db::table('wa_payroll_requisition_examine_approve')->where($condition)->update($data);
    }

    /**
     * 采购请款审批-查询
     * @param $data
     * @return int
     */
    public static function find($adminId,$condition)
    {
        $res = Db::table('wa_payroll_requisition_examine_approve')
            ->where($condition)
            ->first();
        if ($res){
            $approve = WaPayrollRequisitionExamineApproveFlow::all(
                ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'status' => 3]);
            $approve = json_decode($approve);
            if ($approve){
                $approve_status = 3; // 已撤回
                $approve_status_name = '已撤回';

            }else{
                $approve = WaPayrollRequisitionExamineApproveFlow::all(
                    ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'department_id' => $adminId->department_id,'status' => 2]);
//                ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'status' => 2]);
                $approve = json_decode($approve);
                if ($approve){
                    $approve_status = 2; // 已驳回
                    $approve_status_name = '已驳回';
                }else{
                    $approve = WaPayrollRequisitionExamineApproveFlow::all(
                        ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'department_id' => $adminId->department_id,'status' => 1]);
//                    ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'status' => 1]);
                    $approve = json_decode($approve);
                    if ($approve){
                        $approve_status = 1; // 已审批
                        $approve_status_name = '已审批';
                    }else{
                        $approve = WaPayrollRequisitionExamineApproveFlow::all(
                            ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'department_id' => $adminId->department_id,'status' => 0]);
//                        ['purchase_payroll_sn' => $res->purchase_payroll_sn,'pr_no' => $res->pr_no,'status' => 0]);
                        $approve = json_decode($approve);
                        if ($approve){
                            $approve_status = 0; // 待审批
                            $approve_status_name = '待审批';
                        }else{
                            $approve_status = 999;// 待提交审批
                            $approve_status_name = '待提交审批';
                        }
                    }
                }

            }
            $res->approve_status = $approve_status; // 审批状态
            $res->approve_status_name = $approve_status_name;// 审批状态名称
        }
        return json_encode($res);
    }


    public static function findApproveStatusByPrNo($condition)
    {
        $res = Db::table('wa_payroll_requisition_examine_approve')
            ->where($condition)
            ->first();
        if ($res){
        }
        return json_encode($res);
    }


    /**
     * 采购请款审批 -创建
     * @param $data
     * @return int
     */
    public static function create($data)
    {
        return Db::table('wa_payroll_requisition_examine_approve')->insertGetId($data);
    }


    /**
     * 采购请款审批 -删除
     * @param $data
     * @return int
     */
    public static function deleted($condition)
    {
        return Db::table('wa_payroll_requisition_examine_approve')->where($condition)->delete();
    }

    public static function all($condition)
    {
        $pageNum = 10;
        $condition [] = ['is_show','=',1];
        $list = Db::table('wa_payroll_requisition_examine_approve')
            ->select('*')
            ->where($condition)
            ->get();

        return json_encode($list);
    }

}